package com.example.rfidserver.server;


import com.example.rfidserver.dao.UserDao;
import com.example.rfidserver.pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerHelper {

    public void startServer() throws Exception {
        int port = 55555;
        final ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("server waiting....");
        //接受线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Socket socket = serverSocket.accept();
                        InputStream inputStream = socket.getInputStream();
                        byte [] bts = new byte[1024];
                        int len;
                        StringBuffer sb = new StringBuffer();
                        while((len = inputStream.read(bts))!=-1){
                            sb.append(new String(bts, 0, len,"UTF-8"));
                        }
                        System.out.println("服务器接受到了消息" + sb);
                        //如果以requestuserinfo打头，那么是客户端的代码。截取之后发送响应。
                        if(sb.toString().startsWith("requestuserinfo")){
                            long userid = Long.parseLong(sb.substring(sb.indexOf(":")+1));
                            System.out.println("用户的身份证号码为:"+userid);
                            UserDao userDao = new UserDao();
                            User user = userDao.getUserIntegralByID(userid);
                            double bill = user.getIntegral_bill();
                            System.out.println("bill:"+bill);
                            OutputStream outputStream = socket.getOutputStream();
                            outputStream.write(String.valueOf(Math.floor(bill)).getBytes("UTF-8"));
                            outputStream.close();
                        }else if(sb.toString().startsWith("requestuserupdate")){
                            long userid = Long.parseLong(sb.substring((sb.indexOf(":")+1),sb.lastIndexOf(":")));
                            //服务号是为了判断志愿者服务版本，方便更新的
                            int service = Integer.parseInt(sb.substring(sb.lastIndexOf(":")+1));
                            System.out.println("用户id为:" + userid);
                            System.out.println("请求服务为:"+service);
                            //执行数据库的更新操作
                            UserDao userDao = new UserDao();
                            userDao.updateVolunteerByID(userid);
                        }
                        //否则就是处理RFID卡。这一步要收到卡号，然后转化一下。
                        else{
                            //1.获得数据
                            String data = sb.toString();
                            //类似于这种格式:0D 00 96 71 8C 66 XX
                            //[注]:最后一个字节是为了区分服务
                            //RFID的卡号
                            String r_number = sb.substring(0,12);
                            System.out.println("卡号是:" + r_number);
                            //区分服务
                            String r_service = sb.substring(12);
                            System.out.println("区分服务:" + r_service);
                            //规定一下区分服务的细节
                            /*                      consumeID
                            * 01,register               1
                            * 02,Boating                2
                            * 03,show                   3
                            * 04,sightseeing bus        4
                            * */
                            //调用相应的数据库服务，进行扣除。传递参数，RFID卡号，以及服务。这个服务不是0102而是右边的。
                            UserDao userDao = new UserDao();
                            //注意，这个服务是减积分的。还有志愿者是加积分的。要 改一下sql或其他
                            userDao.updateUserIntegralByR_ID(r_number,r_service.substring(1));


                        }



                        inputStream.close();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }//while循环结束

            }
        }).start();
    }


}
